<?php
use app\models\Article;
use app\models\KeyMap;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\Pjax;

/**
 * @var $this yii\web\View
 * @var $model_list app\models\Article[]
 * @var $pagination yii\data\Pagination
 */

$this->title = '资讯审核';
$this->params['breadcrumbs'][] = '资讯管理';
$this->params['breadcrumbs'][] = $this->title;

app\assets\TableAsset::register($this);
app\assets\AjaxAsset::register($this);
?>
<table id="simple-table" class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl"></span>
            </label>
        </th>
        <th>分类</th>
        <th>标题</th>
        <th>状态</th>
        <th>添加时间</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($model_list as $model) {?>
        <tr id="data_<?php echo $model->id;?>">
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $model->id;?>" /><span class="lbl"><?php echo $model->id;?></span></label></td>
            <td><?php echo empty($model->category) ? $model->cid : $model->category->name;?></td>
            <td><?php echo Html::encode($model->title);?></td>
            <td><?php echo Html::a(KeyMap::getValue('article_status', $model->status), 'javascript:void(0)', ['class'=>[
                    Article::STATUS_DRAFT=>'label label-default',
                    Article::STATUS_WAIT=>'label label-info',
                    Article::STATUS_PUBLISHED=>'label label-success',
                    Article::STATUS_REFUSED=>'label label-warning',
                    Article::STATUS_DELETED=>'label label-error'
                ][$model->status]]);?></td>
            <td><?php echo Yii::$app->formatter->asDatetime($model->add_time);?></td>
            <td><?php echo app\widgets\AdminTableOp::widget(['items'=>[
                    ['icon' => 'fa fa-info', 'btn_class'=>'btn btn-xs btn-default', 'tip'=>'详情', 'onclick'=>'showArticle(' . $model->id . ')'],
                ]]);?></td>
        </tr>
    <?php }?>
    </tbody>
</table>
<?php Pjax::begin(['id'=>'pjax_article_detail']);?>
<?php if (Yii::$app->request->get('id') > 0) {
    $article = Article::findOne(Yii::$app->request->get('id'));?>
    <div id="article-detail-modal" class="modal" tabindex="-1">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h3 class="smaller lighter blue no-margin"><?php echo Html::encode($article->title);?></h3>
                </div>
                <div class="modal-body"><?php echo $article->content;?></div>
                <div class="modal-footer">
                    <button class="btn btn-sm btn-danger pull-right" data-dismiss="modal">
                        <i class="ace-icon fa fa-times"></i>
                        取消
                    </button>
                    <button class="btn btn-sm btn-warning pull-right" id="verify_refuse">
                        <i class="ace-icon fa fa-trash"></i>
                        审核拒绝
                    </button>
                    <button class="btn btn-sm btn-success pull-right" id="verify_accept">
                        <i class="ace-icon fa fa-check"></i>
                        审核通过
                    </button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
    <script>
        $('#article-detail-modal').modal();
        $('#verify_accept').click(function() {
            $.getJSON('<?php echo Url::to(['/admin/article/publish']);?>', {'id':<?php echo $article->id;?>}, function(json) {
                if (callback(json)) {
                    window.location.reload();
                }
            });
        });
        $('#verify_refuse').click(function() {
            var remark = prompt('请输入拒绝原因：', '');
            if (remark == null || remark == '') {
                return false;
            }
            $.getJSON('<?php echo Url::to(['/admin/article/refuse']);?>', {'id':<?php echo $article->id;?>, 'remark':remark}, function(json) {
                if (callback(json)) {
                    window.location.reload();
                }
            });
        })
    </script>
<?php }?>
<?php Pjax::end();?>
<?php echo app\widgets\LinkPager::widget(['pagination'=>$pagination]);?>
<script>
/**
 * 显示资讯详情
 * 使用PJAX
 * @param id
 */
 function showArticle(id) {
    $.pjax.reload({
        container:'#pjax_article_detail',
        history:false,
        data:{'id':id},
        timeout:10000
});
}
</script>
